package com.time;

import org.junit.Test;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import static java.time.temporal.ChronoField.NANO_OF_SECOND;

public class LocalDateTimeDemo {
    @Test
    public void test01(){
        LocalDateTime localDateTime = LocalDateTime.of(2020,9,9,20,8);
        System.out.println(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
        System.out.println(localDateTime.getLong(NANO_OF_SECOND));
        System.out.println(LocalDateTime.now().get(NANO_OF_SECOND));

    }


    //查看两个时间的差距,只能查询同年情况下的月份距离
    @Test
    public void test02(){
        LocalDateTime localDateTime1 = LocalDateTime.of(2019, 01, 02, 12, 13);
        LocalDateTime localDateTime2 = LocalDateTime.of(2018, 01, 02, 12, 13);
        int months = Period.between(localDateTime1.toLocalDate(), localDateTime2.toLocalDate()).getMonths();
        int years = Period.between(localDateTime1.toLocalDate(), localDateTime2.toLocalDate()).getYears();
        System.out.println(years);
        System.out.println(months);

    }

    /**
     * 计算两个时间的月份差
     */
    @Test
    public void test03(){
        System.out.println(LocalDate.now().until(LocalDate.of(2021,1,1), ChronoUnit.MONTHS));
        System.out.println(LocalDate.of(2023,1,2).until(LocalDate.of(2022,1,2), ChronoUnit.MONTHS));
    }

    @Test
    public void test04(){
        LocalDate localDate = LocalDate.of(2020,1,1);
        System.out.println(localDate);
    }

    //当前日期星期几
    @Test
    public void test05(){
        LocalDate now = LocalDate.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        System.out.println(dayOfWeek);//WEDNESDAY
        //下周2
        LocalDate last2 = now.with(TemporalAdjusters.next(DayOfWeek.of(2)));
        System.out.println(last2);
    }
}
